package com.sgu6.sgu6_machine.ui.fragment.buy;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.sgu6.middleware.event.MachineStatusEvent;
import com.sgu6.middleware.manager.ThreadManager;
import com.sgu6.middleware.util.GenerateUtils;
import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.base.BaseF;
import com.sgu6.sgu6_machine.common.Constant;
import com.sgu6.sgu6_machine.config.MachineConfig;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.dto.GoodsSimpleDto;
import com.sgu6.sgu6_machine.dto.PayDto;
import com.sgu6.sgu6_machine.event.SelectBuyNoEvent;
import com.sgu6.sgu6_machine.event.SelectTypeEvent;
import com.sgu6.sgu6_machine.presenter.MachineP;
import com.sgu6.sgu6_machine.util.QRCodeUtil;
import com.wang.avi.AVLoadingIndicatorView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 二维码支付
 */
public class PayCodeF extends BaseF {

    @BindView(R.id.iv_qrcode)
    ImageView ivQrCode;
    @BindView(R.id.loading)
    AVLoadingIndicatorView progress;
    @BindView(R.id.rl_code_content)
    RelativeLayout rlCodeContent;
    @BindView(R.id.label)
    TextView label;

    private GoodsSimpleDto goodsSimpleDto;
    private MachineP machineP = new MachineP();

    public static Fragment newInstance(String params) {
        return new PayCodeF();
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_pay_code, container, false);

        unbinder = ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBuyEvent(SelectBuyNoEvent event) {
        updateQrCode(event.buyNo);
    }

    /**
     * 更新二维码
     *
     * @param buyNo 购买数量
     */
    public void updateQrCode(int buyNo) {
        String orderNo = GenerateUtils.createOrderNo();
        String url = MachineContainer.machineConfigDto.payDomain + "/cart?orderNo=" + orderNo + "&machineNo=" + MachineContainer.machineConfigDto.machineNo + "&goodsId=" + goodsSimpleDto.getId() + "&goodsNum=" + buyNo + "&merchantId=" + MachineContainer.machineConfigDto.merchantId;
        ivQrCode.setImageBitmap(QRCodeUtil.createQRCodeBitmap(url, 300, 300));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSelectTypeEvent(SelectTypeEvent event) {
        goodsSimpleDto = event.goodsSimpleDto;
        updateQrCode(1);
        if (event.goodsSimpleDto.getCategoryId().equals(Constant.WATER)){
            updateQrCode(5);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMachineStatusEvent(MachineStatusEvent event) {
        MachineContainer.setMachineStatus(event.statusCode);
        switch (event.statusCode) {
            case 0: //离线
                progress.setVisibility(View.VISIBLE);
                break;
            case 1: //待机
                progress.setVisibility(View.INVISIBLE);
                break;
        }
    }

}
